package com.example.test2;

import com.example.bean.Person;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class Demo2 {

    public static void main(String[] args) {

        List<String> users = new ArrayList<>();
        users.add("张三");
        users.add("李四");
        users.add("王五");
        Stream<String> stream = users.stream();
        // 统计
        //long count = stream.count();
        //System.out.println(count);
        // 遍历
        //stream.forEach(System.out::println);
        // 过滤
        //Stream<String> stream1 = stream.filter(s -> s.startsWith("李"));
        //stream1.forEach(System.out::println);
        // 限制
        //Stream<String> stream2 = stream.limit(2);
        //stream2.forEach(System.out::println);
        // 跳过前面元素
        //Stream<String> stream3 = stream.skip(1);
        //stream3.forEach(System.out::println);
        // 分页查询
        //Stream<String> stream4 = stream.skip(1).limit(2);
        //stream4.forEach(System.out::println);
        List<String> nums = new ArrayList<>();
        nums.add("1");
        nums.add("21");
        nums.add("3");
        nums.add("3");
        Stream<String> streamN = nums.stream();
        /*
        // map映射，sorted排序(升序)
        streamN
                //.map(s -> Integer.parseInt(s))
                .map(Integer::parseInt)
                .sorted((o1, o2) -> o2 - o1)
                .forEach(System.out::println);
         */
        // 去重
        //streamN.distinct().forEach(System.out::println);
        List<Person> people = new ArrayList<>();
        people.add(new Person("张三", 18));
        people.add(new Person("张三", 18));
        people.add(new Person("李四", 21));
        Stream<Person> stream1 = people.stream();
        //stream1.distinct().forEach(System.out::println);
        // 匹配
        //System.out.println(stream1.anyMatch(t -> t.getName().equals("张三")));
        // 查找
        //stream1.findFirst()
        // 最大值
        //Optional<Person> min = stream1.min(Comparator.comparingInt(Person::getAge));
        //min.ifPresent(System.out::println);
        // 最大值
        //Optional<Person> max = stream1.max(Comparator.comparingInt(Person::getAge));
        //max.ifPresent(System.out::println);
        /*
        Integer sum = Stream.of(4, 5, 3, 9)
                // identity是默认值,x是sum,y是数组变量
                .reduce(0, (x, y) -> {
                    System.out.println("x=" + x + "|y=" + y);
                    return x + y;
                });
        System.out.println(sum);
         */
        Integer sum = stream1
                .map(Person::getAge)
                .reduce(0, Integer::sum);
        System.out.println(sum);
    }
}
